﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public delegate void LoadDataHandler(PagingParameter pagingParams);
public delegate string GetPagerUrlHandler(int currengPageIndex);

public partial class allergy_PagerControl : System.Web.UI.UserControl
{
    private LoadDataHandler _loadData;
    public LoadDataHandler LoadData
    {
        get { return _loadData; }
        set { _loadData = value; }
    }

    private GetPagerUrlHandler _getPagerUrl;
    public GetPagerUrlHandler GetPagerUrl
    {
        get { return _getPagerUrl; }
        set { _getPagerUrl = value; }
    }

    public PagingParameter PagingParams
    {
        get {
            object obj = ViewState[this.ID + "_PagingParams"];
            if (obj == null)
            {
                obj = new PagingParameter();
                ViewState[this.ID + "_PagingParams"] = obj;
            }
            return (PagingParameter)obj;
        }
        set {
            ViewState[this.ID + "_PagingParams"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < PagingParams.PageCount; i++)
        {
            string url = (this.GetPagerUrl == null) ? "#" : this.GetPagerUrl(i);
            builder.Append("<span>");
            builder.Append("<a href='" + url + "'>").Append((i+1).ToString()).Append("</a>");
            builder.Append("</span>");
        }

        divPager.InnerHtml = builder.ToString();

        ControlPager();
    }

    private void ControlPager()
    {
        if (PagingParams.PageCount == 1)
        {
            divPager.Visible = false;
            divnext.Visible = false;
            divpre.Visible = false;
        }
        else
        {
            divPager.Visible = true;
            divnext.Visible = true;
            divpre.Visible = true;
            img_prev.Enabled = true;
            btn_prev.Enabled = true;
            img_prev.ImageUrl = "images/4-t5.png";
            img_next.ImageUrl = "images/4-t6.png";
            img_next.Enabled = true;
            btn_next.Enabled = true;

            if (PagingParams.CurrentPageIndex == 0)
            {
                img_prev.Enabled = false;
                img_prev.ImageUrl = "images/4-t5pre.png";
                btn_prev.Enabled = false;
            }

            if (PagingParams.CurrentPageIndex == PagingParams.PageCount - 1)
            {
                img_next.Enabled = false;
                img_next.ImageUrl = "images/4-t6next.png";
                btn_next.Enabled = false;
            }
        }
    }

    protected void page_Command(object sender, CommandEventArgs e)
    {
        PagingParameter pagingParams = this.PagingParams;

        if (pagingParams == null)
        {
            if (this.LoadData != null)
            {
                LoadData(pagingParams);
                return;
            }
        }

        if (e.CommandName.Equals("next") || e.CommandName.Equals("nextp"))
        {
            pagingParams.CurrentPageIndex++;
            if (pagingParams.CurrentPageIndex > 0 && pagingParams.CurrentPageIndex < pagingParams.PageCount)
            {
                if (this.LoadData != null)
                    LoadData(pagingParams);
            }
        }
        else
        {
            pagingParams.CurrentPageIndex--;
            if (pagingParams.CurrentPageIndex >= 0 && pagingParams.CurrentPageIndex < pagingParams.PageCount)
            {
                if (this.LoadData != null)
                    LoadData(pagingParams);
            }
        }

        ControlPager();
    }
}